const myAxios = (config) => {
  return new Promise((resolve, reject) => {
    const { method, url, data, params } = config
     const np= setParams(params)
    const xhr = new XMLHttpRequest()
    if (method.toUpperCase() === 'GET') {
      xhr.open('GET', params?url+'?'+np:url)
    xhr.send()
    }
    else {
      xhr.open('POST', url)
      const flag = Object.prototype.toString.call(data) ==='[object Object]'
      xhr.setRequestHeader('ConTent-Type',flag?'application/json':'application/x-www-form-urlencoded')
      xhr.send(flag?JSON.stringify(data):data)
    }


    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300) {
           resolve(JSON.parse(xhr.responseText))
        } else {
          reject('请求失败错误'+xhr.status)
        }
      }
    }
  })
      
    }
function setParams(obj) {
     const keyArr =[]
  for (let key in obj) {
         keyArr.push(`${key}=${obj[key]}`)
  }
    
   return keyArr.join('&')
}
